
import {
  bookCategoryList
} from "../../../utils/commondata.js";

import {
  request
} from "../../../utils/request.js";
import regeneratorRuntime from '../../../utils/runtime';

var appInst =  getApp();



Page({

  /**
   * 页面的初始数据
   */
  data: {
    mainColor:appInst.globalData.mainColor,
    chapterInfo:{},
    leftFlag:false,
    contentType:'poetry',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let pages = getCurrentPages();
    let currentPage = pages[pages.length - 1];
    let options = currentPage.options;
    const { chapterId ,leftFlag,contentType} = options;
    this.setData({
      leftFlag:leftFlag?true:false,
      contentType:contentType?contentType:'poetry',
      chapterId:chapterId,
    });
    this.loadChapterInfo(chapterId);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let title = this.data.chapterInfo.title + "-" + this.data.chapterInfo.authorName?this.data.chapterInfo.authorName:'佚名';
    let shareObj = {
      title: title,
      path: '/pages/chapter/detail/index?isShare=true&chapterId='+this.data.chapterId,

    };
    return shareObj;
  },

  handleMore(e){
    let {index} = e.currentTarget.dataset;
    let contentList = [];

    for(let i=0;i<this.data.chapterInfo.paragraphVOList.length;i++){
      let c = this.data.chapterInfo.paragraphVOList[i];
      if(i == index){
        c.height = 500;
      }else{
        c.height = 10;
      }
      contentList.push(c);
    }
    let chapterInfo = this.data.chapterInfo;
    chapterInfo.paragraphVOList = contentList;
    this.setData({
      chapterInfo:chapterInfo
    });
  },
  handleLess(e){
    let contentList = [];
    for(let i=0;i<this.data.chapterInfo.paragraphVOList.length;i++){
      let c = this.data.chapterInfo.paragraphVOList[i];
      c.height = 10;
      contentList.push(c);
    }
    let chapterInfo = this.data.chapterInfo;
    chapterInfo.paragraphVOList = contentList;
    this.setData({
      chapterInfo:chapterInfo
    });
  },
  handleIntroduceMore(e){
    let {index} = e.currentTarget.dataset;
    let contentList = [];

    for(let i=0;i<this.data.chapterInfo.introduceVOList.length;i++){
      let c = this.data.chapterInfo.introduceVOList[i];
      if(i == index){
        c.height = 50;
      }else{
        c.height = 10;
      }
      contentList.push(c);
    }
    let chapterInfo = this.data.chapterInfo;
    chapterInfo.introduceVOList = contentList;
    this.setData({
      chapterInfo:chapterInfo
    });
  },
  handleIntroduceLess(e){
    let contentList = [];
    for(let i=0;i<this.data.chapterInfo.introduceVOList.length;i++){
      let c = this.data.chapterInfo.introduceVOList[i];
      c.height = 10;
      contentList.push(c);
    }
    let chapterInfo = this.data.chapterInfo;
    chapterInfo.introduceVOList = contentList;
    this.setData({
      chapterInfo:chapterInfo
    });
  },
  /**
   * 查询章节明细
   * 
   * @param {*} chapterId 
   */
  async loadChapterInfo(chapterId){
    const res = await request({
      url: "/chapter/detail?id=" + chapterId,
      method: "get"
    });
    if(res.code != 0){
      return;
    }
    let paragraphVOList = [];
    let chapterInfo = res.data;
    res.data.paragraphVOList.forEach(element => {
      element.height=10;
      paragraphVOList.push(element);
    });

    let introduceVOList = [];
    res.data.introduceVOList.forEach(element => {
      element.height=10;
      introduceVOList.push(element);
    });

    chapterInfo.paragraphVOList = paragraphVOList;
    chapterInfo.introduceVOList = introduceVOList;
    this.setData({
      chapterInfo:chapterInfo,
    });
  },
})